import client from "../../utils/pool.mjs"
import { jsonResponse, httpCode } from "@/utils/index.mjs";
import { sendVerificationCode, generateRandomFourDigit } from '@/service/VerificationCode.mjs';

export async function POST(request) {

    // 从body中获取phone和pwd
    const formData = await request.json()
    const phone = formData.phone
    const scene = formData.scene
    // 校验phone是否是合法手机号码
    if (!/^1[3456789]\d{9}$/.test(phone)) {
        return jsonResponse(400, {
            message: '手机号码格式错误'
        })
    }
    // 从user库里面查询phone是否存在，存在的话再对比密码；使用ORM方法
    const user = await client.user.findFirst({
        where: {
            phone: phone.toString()
        }
    })
    if (!user) {
        return jsonResponse(httpCode.BUSINESS_ERROR, {
            message: '手机号码不存在'
        })
    }
    const code = generateRandomFourDigit();
    const res = await sendVerificationCode(17608015960, scene, generateRandomFourDigit(), 60 * 1000)
    console.log('--验证码发送成功: ', code)
    return jsonResponse(200, { success: true })
}
